package com.sc.ktapplication.common.base

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.sc.ktapplication.R
import com.sc.ktapplication.common.util.KeyboardUtils

abstract class BaseListActivity : BaseActivity() {

    open lateinit var recyclerView: RecyclerView
    open var refreshLayout: SwipeRefreshLayout? = null
    open var etSearch: EditText? = null
    open var emptyView: View? = null
    open lateinit var mAdapter: BaseQuickAdapter<*,BaseViewHolder>

    open var pageIndex = 1
    open var pageSize = 20

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        findViews()
        emptyView = layoutInflater.inflate(R.layout.empty_view, recyclerView.parent as ViewGroup, false)
        refreshLayout!!.setColorSchemeResources(R.color.light_blue)
        refreshLayout!!.setOnRefreshListener { refreshData() }
        initAdapter()
        if (isNeedEmptyView()) mAdapter.setEmptyView(emptyView!!)
        mAdapter.loadMoreModule.isEnableLoadMore = true
        if (isNeedLoadMore()) {
            mAdapter.loadMoreModule.setOnLoadMoreListener(listener = {
                pageIndex++
                loadData()
            })
        }
        mAdapter.setOnItemClickListener { adapter: BaseQuickAdapter<*, *>?, view: View, position: Int ->
            onItemClick(adapter, position)
        }
        recyclerView.layoutManager = LinearLayoutManager(applicationContext)
        recyclerView.adapter = mAdapter
        initGrid()
        if (null != etSearch) {
            etSearch!!.setOnEditorActionListener { v: TextView?, actionId: Int, event: KeyEvent? ->
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    refreshData()
                    KeyboardUtils.hideSoftInput(this)
                }
                true
            }
        }
        refreshData()
    }

    protected fun setEmptyText(s: String?) {
        val txtEmpty = emptyView!!.findViewById<TextView>(R.id.txtEmpty)
        txtEmpty.text = s
    }

    open fun refreshData() {
        refreshLayout!!.isRefreshing = true
        pageIndex = 1
        loadData()
    }

    open fun completeLoad(lastPage: Boolean) {
        refreshLayout!!.isRefreshing = false
        mAdapter.loadMoreModule.loadMoreComplete()
        if (lastPage) {
            mAdapter.loadMoreModule.loadMoreEnd()
        }
    }

    open fun initGrid() {}

    abstract fun loadData()

    abstract fun initAdapter()

    abstract fun onItemClick(adapter: BaseQuickAdapter<*, *>?, position: Int)

    open fun isNeedEmptyView() : Boolean{
        return true
    }

    open fun isNeedLoadMore() : Boolean{
        return true
    }

    override fun findViews() {
        super.findViews()
        recyclerView = findViewById(R.id.recyclerView)
        refreshLayout = findViewById(R.id.refresh)
        etSearch = findViewById(R.id.etSearch)
    }
}